package chinaums.service.impl;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import chinaums.common.util.DateUtil;
import chinaums.dao.ConfigureMapper;
import chinaums.entity.CodeMsg;
import chinaums.service.IConfigureService;

@Service
public class ConfigureServiceImpl implements IConfigureService{
	@Autowired
	private ConfigureMapper configureMapper;
	
	@Override
	public long dologin(Map<String, Object> map) {
		
		return configureMapper.dologin(map);
	}

	@Override
	public CodeMsg insertExceltoDateBase(String resString,String loginuser) throws IOException {
		CodeMsg msg = new CodeMsg();
		boolean isExcel2003 = true;
		if (isExcel2007(resString))//判断是xls还是xlsx
		{
			isExcel2003 = false;
		}
		File excelFile = new File(resString); //创建文件对象  
	     FileInputStream is = new FileInputStream(excelFile); //文件流  
	     
	     is = new FileInputStream(excelFile); //文件流
	     
				if(isExcel2003){
					HSSFWorkbook wb = new HSSFWorkbook(is);
					msg=insertExcelContent(is,loginuser,wb);
				}else{
					XSSFWorkbook wb = new XSSFWorkbook(is);
					msg=insertExcelContent(is,loginuser,wb);
				}
	     
		
		return msg;
	}

	
	@SuppressWarnings("deprecation")
	private CodeMsg insertExcelContent(FileInputStream is, String loginuser,
			Workbook wb) {
		CodeMsg msg = new CodeMsg();
		Sheet sheet = wb.getSheetAt(0);//默认获取第一个sheet
		int containurl = 0;
		String allsendcnt = "";
		String singlesendcnt = "";
		Row firstrow = sheet.getRow(0);//获取第一行
		int cellnum = firstrow.getLastCellNum();
		if(cellnum <8){
			msg.setCode("fail");
			msg.setMsg("列数错误！");
			return msg;
		}
		Map<String,Object> param = new HashMap<String,Object>();
		//检查文件头。看是否符合规范
				firstrow.getCell(0).setCellType(Cell.CELL_TYPE_STRING);//开始检查文件头，是否和要求相同
				if(firstrow.getCell(0)==null){
					msg.setCode("fail");
					msg.setMsg("营销物料配置文件内容不全");
					return msg;
				}else{
					String firstcellname=firstrow.getCell(0).getStringCellValue();
					if(!"客户ID".equals(firstcellname)){
						msg.setCode("fail");
						msg.setMsg("营销物料配置文件内容不全");
						return msg;
					}
				}
				
				if(firstrow.getCell(1)==null){
					msg.setCode("fail");
					msg.setMsg("营销物料配置文件内容不全");
					return msg;
				}else{
					firstrow.getCell(1).setCellType(Cell.CELL_TYPE_STRING);
					String secondcellname=firstrow.getCell(1).getStringCellValue();
					if(!"客户名称".equals(secondcellname)){
						msg.setCode("fail");
						msg.setMsg("营销物料配置文件内容不全");
						return msg;
					}
				}
				if(firstrow.getCell(2)==null){
					msg.setCode("fail");
					msg.setMsg("营销物料配置文件内容不全");
					return msg;
				}else{
					firstrow.getCell(2).setCellType(Cell.CELL_TYPE_STRING);
					String thirdcellname=firstrow.getCell(2).getStringCellValue();
					if(!"模板编号".equals(thirdcellname)){
						msg.setCode("fail");
						msg.setMsg("营销物料配置文件内容不全");
						return msg;
					}
				}
				if(firstrow.getCell(3)==null){
					msg.setCode("fail");
					msg.setMsg("营销物料配置文件内容不全");
					return msg;
				}else{
					firstrow.getCell(3).setCellType(Cell.CELL_TYPE_STRING);
					String forthcellname=firstrow.getCell(3).getStringCellValue();
					if(!"URL链接".equals(forthcellname)){
						msg.setCode("fail");
						msg.setMsg("营销物料配置文件内容不全");
						return msg;
					}
				}
				
				if(firstrow.getCell(4)==null){
					msg.setCode("fail");
					msg.setMsg("营销物料配置文件内容不全");
					return msg;
				}else{
					firstrow.getCell(4).setCellType(Cell.CELL_TYPE_STRING);
					String firthcellname=firstrow.getCell(4).getStringCellValue();
					if(!"触发规则编号".equals(firthcellname)){
						msg.setCode("fail");
						msg.setMsg("营销物料配置文件内容不全");
						return msg;
					}
				}
				
				if(firstrow.getCell(5)==null){
					msg.setCode("fail");
					msg.setMsg("营销物料配置文件内容不全");
					return msg;
				}else{
					firstrow.getCell(5).setCellType(Cell.CELL_TYPE_STRING);
					String sixthcellname=firstrow.getCell(5).getStringCellValue();
					if(!"营销周期".equals(sixthcellname)){
						msg.setCode("fail");
						msg.setMsg("营销物料配置文件内容不全");
						return msg;
					}
				}
				
				if(firstrow.getCell(6)==null){
					msg.setCode("fail");
					msg.setMsg("营销物料配置文件内容不全");
					return msg;
				}else{
					firstrow.getCell(6).setCellType(Cell.CELL_TYPE_STRING);
					String seventhcellname=firstrow.getCell(6).getStringCellValue();
					if(!"总发送数量".equals(seventhcellname)){
						msg.setCode("fail");
						msg.setMsg("营销物料配置文件内容不全");
						return msg;
					}
				}
				
				if(firstrow.getCell(7)==null){
					msg.setCode("fail");
					msg.setMsg("营销物料配置文件内容不全");
					return msg;
				}else{
					firstrow.getCell(7).setCellType(Cell.CELL_TYPE_STRING);
					String eighthcellname=firstrow.getCell(7).getStringCellValue();
					if(!"个体发送数量".equals(eighthcellname)){
						msg.setCode("fail");
						msg.setMsg("营销物料配置文件内容不全");
						return msg;
					}
				}
				
				if(firstrow.getCell(8)==null){
					msg.setCode("fail");
					msg.setMsg("营销物料配置文件内容不全");
					return msg;
				}else{
					firstrow.getCell(8).setCellType(Cell.CELL_TYPE_STRING);
					String ninthcellname=firstrow.getCell(8).getStringCellValue();
					if(!"个体发送间隔(分钟)".equals(ninthcellname)){
						msg.setCode("fail");
						msg.setMsg("营销物料配置文件内容不全");
						return msg;
					}
				}
				
				
				
				
				int z = sheet.getLastRowNum();
				if(z<1){
					msg.setCode("fail");
					msg.setMsg("营销物料配置文件内容为空");
					return msg;
				}
				Row secondrow = sheet.getRow(1);//拿出数据
				if(secondrow.getCell(0)==null){
					msg.setCode("fail");
					msg.setMsg("营销物料配置文件内容不全");
					return msg;
				}else{
					secondrow.getCell(0).setCellType(Cell.CELL_TYPE_STRING);
					String CUSTOM_ID=secondrow.getCell(0).getStringCellValue();//获取客户ID
					if("".equals(CUSTOM_ID)){
						msg.setCode("fail");
						msg.setMsg("营销物料配置文件内容不全");
						return msg;
					}
					Pattern p = Pattern.compile("[\u4e00-\u9fa5]");
			        Matcher m = p.matcher(CUSTOM_ID);
					if(m.find()){
						msg.setCode("fail");
						msg.setMsg("客户ID不符合校验规则");
						return msg;
					}
					if(CUSTOM_ID.length()>20){
						msg.setCode("fail");
						msg.setMsg("客户ID不符合校验规则");
						return msg;
					}
					
					
					
					param.put("CUSTOM_ID", CUSTOM_ID);
				}
				
				if(secondrow.getCell(1)==null){
					msg.setCode("fail");
					msg.setMsg("营销物料配置文件内容不全");
					return msg;
				}else{
					secondrow.getCell(1).setCellType(Cell.CELL_TYPE_STRING);
					String CUSTOM_NAME=secondrow.getCell(1).getStringCellValue();//获取客户名
					if("".equals(CUSTOM_NAME)){
						msg.setCode("fail");
						msg.setMsg("营销物料配置文件内容不全");
						return msg;
					}
					param.put("CUSTOM_NAME", CUSTOM_NAME);
				}
				
				if(secondrow.getCell(2)==null){
					msg.setCode("fail");
					msg.setMsg("营销物料配置文件内容不全");
					return msg;
				}else{
					secondrow.getCell(2).setCellType(Cell.CELL_TYPE_STRING);
					String TEMPLATEID =secondrow.getCell(2).getStringCellValue();//获取消息模板编号
					if("".equals(TEMPLATEID)){
						msg.setCode("fail");
						msg.setMsg("营销物料配置文件内容不全");
						return msg;
					}
					Pattern p = Pattern.compile("[\u4e00-\u9fa5]");
			        Matcher m = p.matcher(TEMPLATEID);
					if(m.find()){
						msg.setCode("fail");
						msg.setMsg("模板编号不符合校验规则");
						return msg;
					}
					if(TEMPLATEID.length()>20){
						msg.setCode("fail");
						msg.setMsg("模板编号不符合校验规则");
						return msg;
					}
					
					param.put("TEMPLATEID", TEMPLATEID);
				}
				if(secondrow.getCell(3)==null){
					param.put("URL", "");
				}else{
					secondrow.getCell(3).setCellType(Cell.CELL_TYPE_STRING);
					String URL=secondrow.getCell(3).getStringCellValue();//获取URL
					if("".equals(URL)){
						containurl = 0;
					}else{
						containurl = 1;
					}
					
					param.put("URL", URL);
				}
				
				if(secondrow.getCell(4)==null){
					msg.setCode("fail");
					msg.setMsg("营销物料配置文件内容不全");
					return msg;
				}else{
					secondrow.getCell(4).setCellType(Cell.CELL_TYPE_STRING);
					String RULE_NUMBER=secondrow.getCell(4).getStringCellValue();//获取触发规则编号
					
					long cntrule = configureMapper.countrule(RULE_NUMBER);
					if(cntrule>0){
						msg.setCode("fail");
						msg.setMsg("触发规则编号不能重复使用");
						return msg;
					}
					if("".equals(RULE_NUMBER)){
						msg.setCode("fail");
						msg.setMsg("营销物料配置文件内容不全");
						return msg;
					}
					
					String reg = "[A-Za-z0-9]{1,20}";
					if(!RULE_NUMBER.matches(reg)){
						msg.setCode("fail");
						msg.setMsg("规则编号格式错误");
						return msg;
					}
					param.put("RULE_NUMBER", RULE_NUMBER);
				}
				
				if(secondrow.getCell(5)==null){
					msg.setCode("fail");
					msg.setMsg("营销物料配置文件内容不全");
					return msg;
				}else{
					secondrow.getCell(5).setCellType(Cell.CELL_TYPE_STRING);
					String MARKETING_CYCLE=secondrow.getCell(5).getStringCellValue();//获取营销周期
					if("".equals(MARKETING_CYCLE)){
						msg.setCode("fail");
						msg.setMsg("营销物料配置文件内容不全");
						return msg;
					}
					String[] cycle = MARKETING_CYCLE.split("-");
					if(cycle.length!=2){
						msg.setCode("fail");
						msg.setMsg("营销物料配置文件营销周期时间格式错误");
						return msg;
					}
					
					
					String startdate = cycle[0];
					Date start_date = DateUtil.sring2dateyyyyMMdd(startdate);
					if(start_date==null){
						msg.setCode("fail");
						msg.setMsg("营销物料配置文件营销周期开始时间格式错误");
						return msg;
					}
					
					String enddate = cycle[1];
					Date end_date = DateUtil.sring2dateyyyyMMdd(enddate);
					if(end_date==null){
						msg.setCode("fail");
						msg.setMsg("营销物料配置文件营销周期结束时间格式错误");
						return msg;
					}
					
					if(start_date.compareTo(end_date)>0){
						msg.setCode("fail");
						msg.setMsg("营销物料配置文件营销周期数据错误");
						return msg;
					}
					param.put("MARKETING_CYCLE", MARKETING_CYCLE);
				}
				
				
				if(secondrow.getCell(6)==null){
					msg.setCode("fail");
					msg.setMsg("营销物料配置文件内容不全");
					return msg;
				}else{
					secondrow.getCell(6).setCellType(Cell.CELL_TYPE_STRING);
					String ALL_SEND_CNT=secondrow.getCell(6).getStringCellValue();//获取总发送总数
					if("".equals(ALL_SEND_CNT)){
						msg.setCode("fail");
						msg.setMsg("营销物料配置文件内容不全");
						return msg;
					}
					String reg ="[1-9]\\d*|0"; 
					if(!ALL_SEND_CNT.matches(reg)){
						msg.setCode("fail");
						msg.setMsg("总发送数量格式错误");
						return msg;
					}
					allsendcnt = ALL_SEND_CNT;
					param.put("ALL_SEND_CNT", ALL_SEND_CNT);
				}
				
				if(secondrow.getCell(7)==null){
					msg.setCode("fail");
					msg.setMsg("营销物料配置文件内容不全");
					return msg;
				}else{
					secondrow.getCell(7).setCellType(Cell.CELL_TYPE_STRING);
					String SINGLE_SEND_CNT=secondrow.getCell(7).getStringCellValue();//获取个体发送总数
					if("".equals(SINGLE_SEND_CNT)){
						msg.setCode("fail");
						msg.setMsg("营销物料配置文件内容不全");
						return msg;
					}
					String reg ="[1-9]\\d*|0"; 
					if(!SINGLE_SEND_CNT.matches(reg)){
						msg.setCode("fail");
						msg.setMsg("个体发送数量格式错误");
						return msg;
					}
					
					int a = Integer.parseInt(allsendcnt);
					int b = Integer.parseInt(SINGLE_SEND_CNT);
					
					
					if(a<b){
						msg.setCode("fail");
						msg.setMsg("总发送数量小于个体发送数量");
						return msg;
					}
					
					param.put("SINGLE_SEND_CNT", SINGLE_SEND_CNT);
				}
				
				if(secondrow.getCell(8)==null){
					msg.setCode("fail");
					msg.setMsg("营销物料配置文件内容不全");
					return msg;
				}else{
					secondrow.getCell(8).setCellType(Cell.CELL_TYPE_STRING);
					String SINGLE_SEND_CYCLE=secondrow.getCell(8).getStringCellValue();//获取个体发送间隔
					if("".equals(SINGLE_SEND_CYCLE)){
						msg.setCode("fail");
						msg.setMsg("营销物料配置文件内容不全");
						return msg;
					}
					String reg ="[1-9]\\d*|0"; 
					if(!SINGLE_SEND_CYCLE.matches(reg)){
						msg.setCode("fail");
						msg.setMsg("个体发送间隔格式错误");
						return msg;
					}
					param.put("SINGLE_SEND_CYCLE", SINGLE_SEND_CYCLE);
				}
				String id= configureMapper.getNextId();
				param.put("ID", id);
				Map<String,Object> result = new HashMap<String,Object>();
				result.put("id", id);
				result.put("url", containurl);
				long check = configureMapper.insertconfigure(param);
				if(check>0){
					msg.setCode("success");
					msg.setMsg("保存成功");
					msg.setValue(result);
				}else{
					msg.setCode("fail");
					msg.setMsg("保存失败");
					
				}
				
				
		return msg;
	}

	public static boolean isExcel2003(String filePath)  {  
        return filePath.matches("^.+\\.(?i)(xls)$");  
    }  
  
   //@描述：是否是2007的excel，返回true是2007 
   public static boolean isExcel2007(String filePath)  {  
        return filePath.matches("^.+\\.(?i)(xlsx)$");  
    }

	@Override
	public List<Map<String, Object>> gettasklist(Integer limit, Integer offset) {
		Map<String,Object> map =new HashMap<String,Object>();
		map.put("limit", limit+offset);
		map.put("offset", offset);
		List<Map<String, Object>> list = configureMapper.gettasklist(map);
		return list;
	}
	
	@Override
	public long gettasklistcnt(Integer limit, Integer offset) {
		
		return configureMapper.gettasklistcnt();
	}

	@Override
	public String getnumber(String number) {
		configureMapper.getnumber(number);
		return configureMapper.getnumber(number);
	}
	
	@Override
	public List<Map<String, String>> ko() {
		configureMapper.ko();
		return configureMapper.ko();
	}
	
	@Override
	public long update_number(String get_flg) {
		// TODO Auto-generated method stub
		return configureMapper.update_number(get_flg);
	} 
	
	public static void main(String[] args) {
		String allsendcnt ="400"; 
		String SINGLE_SEND_CNT = "2000";
		int a = Integer.parseInt(allsendcnt);
		int b = Integer.parseInt(SINGLE_SEND_CNT);
		
		
		System.out.println(a<b);
	}
	
}
